﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Sys.IDAO;
using CNKI.TPI.Web.Sys.Model;
using KNet.Data.Entity;

namespace CNKI.TPI.Web.Sys.DAO
{
    public class UserDAO : BaseDAO, IUserDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public SYS_CMS_USER GetUser(string userName)
        {
            SYS_CMS_USER result = null;
            try
            {
                Expression<Func<SYS_CMS_USER, bool>> query = c => c.Username == userName;
                IList<SYS_CMS_USER> userList = Context.Get<SYS_CMS_USER>(query);
                if (userList != null && userList.Count > 0)
                {
                    result = userList[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllUser()
        {
            return context.ExecuteSql(SqlBuilder.Build("*", "SYS_CMS_USER"));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_IP> GetUserIP(int userID)
        {
            IList<SYS_CMS_USER_IP> result = null;
            try
            {
                Expression<Func<SYS_CMS_USER_IP, bool>> query = c => c.UrID == userID;
                result = Context.Get<SYS_CMS_USER_IP>(query);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_GROUP> GetUserGroup(int userID)
        {
            IList<SYS_CMS_USER_GROUP> result = null;
            try
            {
                Expression<Func<SYS_CMS_USER_GROUP, bool>> query = c => c.UserID == userID;
                result = Context.Get<SYS_CMS_USER_GROUP>(query);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_GROUP> GetGroup(IEnumerable<int> groupID)
        {
            IList<SYS_CMS_GROUP> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_GROUP> exp = new ExpressionCombine<SYS_CMS_GROUP>();
                foreach (int item in groupID)
                {
                    exp.Or("SysID", OperatorType.Equal, item);
                }
                result = Context.Get<SYS_CMS_GROUP>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_ACL_DB_RIGHT> GetUserDBRight(int userid)
        {
            IList<SYS_CMS_USER_ACL_DB_RIGHT> result = null;
            try
            {
                Expression<Func<SYS_CMS_USER_ACL_DB_RIGHT, bool>> query = c => c.UrID == userid;
                result = Context.Get<SYS_CMS_USER_ACL_DB_RIGHT>(query);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获得密级信息
        /// </summary>
        /// <param name="dbID"></param>
        /// <param name="fieldType"></param>
        /// <returns></returns>
        public IList<SYS_CMS_SECURITY> GetSecurityInfo()
        {
            IList<SYS_CMS_SECURITY> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_SECURITY>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }
    }
}
